﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 养老金支付系统
{
    public partial class FormSBBFLR : FormBase
    {
        public FormSBBFLR()
        {
            InitializeComponent();
        }

        string grbh = string.Empty;
        string zfbh = string.Empty;
        bool falg = false;//补发状态标志

        void init()
        {
            this.Text = "交易失败补发录入";
            //this.grbh = "";
            textBox1.Text = SqlHelper.getScalar("select dbo.GetNextQH(dbo.GetNowQH())") as string;
            //textBox2.Text = "";
            button2.Enabled = false;
        }

        void find()
        {
            
            string sql = @"select 
                支付编号,交易状态码,补发状态码,
                cast(case 
	                when (交易状态码=0 and (补发期号=dbo.GetNextQH(dbo.GetNowQH()) or 补发期号 is null or 补发期号='')) then 1
	                else 0 
	                end as bit) as 能否补发,
                支付期号,支付类型,个人编号,姓名,支付金额,银行名称,银行帐号,
                交易状态,补发状态,补发期号,补发录入时间 as 补发时间
                 from 查支付明细 where 支付期号<=dbo.GetMaxZZQH() and 个人编号=@GRBH 
                order by 支付编号 desc";
            SqlParameter p = new SqlParameter("@GRBH", this.grbh);
            //DataTable dt = SqlHelper.getDataTable(sql, p);

            dataGridView1.DataSource = SqlHelper.getDataTable(sql, p);
            dataGridView1.Columns[0].Visible = false;
            dataGridView1.Columns[1].Visible = false;
            dataGridView1.Columns[2].Visible = false;
            dataGridView1.Columns[3].Visible = false;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (bool.Parse(row.Cells[1].Value.ToString()))
                    row.Cells[11].Style.ForeColor = Color.Green;
                else
                    row.Cells[11].Style.ForeColor = Color.Red;
            }
        }

        void add()
        {
            if (SqlHelper.exeProcReturn("P_失败补发录入", new SqlParameter("@ZFBH", this.zfbh)) > 0)
                MessageBox.Show(this, "补发成功！", "补发", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show(this, "补发失败！", "补发", MessageBoxButtons.OK, MessageBoxIcon.Error);
            
        }

        void del()
        {
            if (SqlHelper.exeProcReturn("P_失败补发删除", new SqlParameter("@ZFBH", this.zfbh)) > 0)
                MessageBox.Show(this, "取消成功！", "取消", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show(this, "取消失败！", "取消", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }
        

        private void FormSBBFLR_Load(object sender, EventArgs e)
        {
            init();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            find();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            this.grbh = textBox2.Text;
        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView1.Rows.Count > 0)
            {
                if (bool.Parse(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString())) //能补发
                {
                    this.falg = bool.Parse(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString());
                    if (this.falg) //已补发
                    {
                        button2.Text = "取  消";
                    }
                    else
                    {
                        button2.Text = "补  发";
                    }
                    this.zfbh = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                    button2.Enabled = true;
                }
                else
                    button2.Enabled = false;
            }
            else
                button2.Enabled = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.zfbh.Length > 0)
            {
                if (falg)//已补发
                    del();
                else
                    add();
                find();
            }
        }

        private void buttonGRBH_Click(object sender, EventArgs e)
        {

            FormGRBHXZ fgrbhxz = new FormGRBHXZ();
            if (fgrbhxz.ShowDialog() == DialogResult.OK)
            {
                this.grbh = fgrbhxz.GRBH;
                textBox2.Text = this.grbh;
                find();
            }
        }
    }
}
